package com.koc.robot;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class KOCActivity extends Activity {
	private BasicGLSurfaceView mView;
	
	private Button mLeftButton;
	private Button mRightButton;
	private Button mUpButton;
	private Button mDownButton;
	
	public static final int SHOW_CONTROL_BUTTONS = 1;
	public static final int HIDE_CONTROL_BUTTONS = 2;
	private Handler mUIUpdateHandle;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
  /*      mView = new BasicGLSurfaceView(getApplication());
        setContentView(mView);
  */
        setContentView(R.layout.main);
        mView = (BasicGLSurfaceView)findViewById(R.id.basicGLSurfaceView1);
        
        Global.main_activity = this;
        
        mLeftButton = (Button)findViewById(R.id.button_left);
        mRightButton = (Button)findViewById(R.id.button_right);
        mUpButton = (Button)findViewById(R.id.button_up);
        mDownButton = (Button)findViewById(R.id.button_down);
   
        mLeftButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	mView.onButtonClicked(R.id.button_left);
            }
        });
        mRightButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	mView.onButtonClicked(R.id.button_right);
            }
        });
        mUpButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	mView.onButtonClicked(R.id.button_up);
            }
        });
        mDownButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	mView.onButtonClicked(R.id.button_down);
            }
        });
        
       mLeftButton.setVisibility(View.INVISIBLE);
       mRightButton.setVisibility(View.INVISIBLE);
       mUpButton.setVisibility(View.INVISIBLE);
       mDownButton.setVisibility(View.INVISIBLE);
       
       mUIUpdateHandle = new UIUpdateHande();
    }
    
    @Override
    public void onDestroy()
    {
    	super.onDestroy();
    }
   
    public void showControlButtons(boolean bshow)
    {
    	int msg = (bshow == true ? SHOW_CONTROL_BUTTONS : HIDE_CONTROL_BUTTONS);
    	mUIUpdateHandle.sendEmptyMessageDelayed(msg, 0);
    }
    
    private class UIUpdateHande extends Handler
    {
        @Override 
        public void handleMessage(Message msg)
        {
        	switch(msg.what)
        	{
        	case SHOW_CONTROL_BUTTONS:
                mLeftButton.setVisibility(View.VISIBLE);
                mRightButton.setVisibility(View.VISIBLE);
                mUpButton.setVisibility(View.VISIBLE);
                mDownButton.setVisibility(View.VISIBLE);
        		break;
        	case HIDE_CONTROL_BUTTONS:
        	    mLeftButton.setVisibility(View.INVISIBLE);
        	    mRightButton.setVisibility(View.INVISIBLE);
        	    mUpButton.setVisibility(View.INVISIBLE);
        	    mDownButton.setVisibility(View.INVISIBLE);
        		break;
        	}
        }
    } ;
}